package site.lanmushan.framework.util.swagger;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Slf4j
public class SwaggerResolving {
    JSONObject swaggerJson;
    JSONObject paths;
    JSONObject definitions;
    String swaggerVersion;
    String basePath;

    public SwaggerResolving(JSONObject swaggerJson) {
        swaggerJson = swaggerJson;
        paths = swaggerJson.getJSONObject("paths");
        definitions = swaggerJson.getJSONObject("definitions");
        swaggerVersion = swaggerJson.getString("swagger");
        basePath = swaggerJson.getString("basePath");
    }

    Map<String, List<SwaggerDto>> resolving() {
        Map<String, List<SwaggerDto>> result = new HashMap<String, List<SwaggerDto>>();
        List<SwaggerDto> swaggerDtoList = new ArrayList<SwaggerDto>();

        if (ObjectUtil.isEmpty(paths)) {
            return null;
        }

        for (Map.Entry<String, Object> entry : paths.entrySet()) {
            List<SwaggerDto> tempList = resolvingPath(entry.getKey(), (JSONObject) entry.getValue());
            swaggerDtoList.addAll(tempList);
            result.putAll(toMap(swaggerDtoList));
        }
        result.putAll(toMap(swaggerDtoList));
        return result;
    }

    private Map<String, ? extends List<SwaggerDto>> toMap(List<SwaggerDto> swaggerDtoList) {
        Map<String, List<SwaggerDto>> result = new HashMap<String, List<SwaggerDto>>();
        Iterator iterate = swaggerDtoList.listIterator();
        while (iterate.hasNext()) {
            SwaggerDto value = (SwaggerDto) iterate.next();
            if (value.tags != null) {
                List<String> tags = value.getTags();
                for (String tag : tags) {
                    if (!result.containsKey(tag)) {
                        List<SwaggerDto> list = new ArrayList<SwaggerDto>();
                        result.put(tag, list);
                    }
                    result.get(tag).add(value);
                }
            }
        }
        return result;
    }

    private List<SwaggerDto> resolvingPath(String path, JSONObject pathSchema) {
        List<SwaggerDto> resultList = new ArrayList<SwaggerDto>();
        for (Map.Entry<String, Object> entry : pathSchema.entrySet()) {
            SwaggerDto dto = new SwaggerDto();
            JSONObject pathValue = (JSONObject) entry.getValue();
            JSONArray tags = pathValue.getJSONArray("tags");
            dto.apiPath = (basePath + path).replace("//", "/");
            if (tags.size() == 0) {
                tags.add("");
            }
            dto.apiName = tags.getString(0) + "-" + pathValue.getString("summary");
            dto.method = entry.getKey();
            if (dto.tags == null) {
                dto.tags = new ArrayList<>();
            }
            dto.tags.addAll((Collection<? extends String>) tags.stream().map(it -> it.toString()).collect(Collectors.toList()));
            resultList.add(dto);
            log.info("数据{}", JSONObject.toJSONString(dto));
        }
        return resultList;
    }

}


